package com.ruc.deke.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Component;

import com.ruc.deke.dao.ModuleDAO;
import com.ruc.deke.model.DBRecord;
import com.ruc.deke.utils.ParseUtil;

/**
 * 具体实现
 * 
 * @author: cyj
 * @date: 2017年3月30日 下午4:19:11
 */

@Component(value = "moduleDAOImpl")
public class ModuleDAOImpl implements ModuleDAO {

	@Resource(name = "dataSource_rucdbcourse")
	private DataSource dataSource;

	@Override
	public ArrayList<DBRecord> getModules() {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();

		try {
			String sql = "select * from db_course_module where status = 1 order by moduleid";
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			statement.close();
			connection.close();
		} catch (SQLException e) {
			// e.printStackTrace();
			System.out.println("exception:" + e.getMessage());
		}

		return records;
	}

	@Override
	public ArrayList<DBRecord> getParentModules() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<DBRecord> getModuleDetails(int moduleId, String userid) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();
		System.out.println("$$$$$$$$$$$");
		// System.out.println(userid);
		try {
			String sql = "select * from exp_module where questionid in "
					+ "(select questionid from paper_exp where paperid in "
					+ "(select paperid from PAPER_INFO,DB_COURSE_USER " + "where userid=" + userid
					+ " and paper_info.ano=db_course_user.ano and isactive=1 and pmoduleid=" + moduleId + ")) " + "order by expid";
			// String sql="select * from exp_module where moduleid = " +
			// moduleId + " order by expid";
			System.out.println(sql);
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			statement.close();
			connection.close();
		} catch (SQLException e) {
			System.out.println("exception:" + e.getMessage());
			// e.printStackTrace();
		}

		return records;
	}
	@Override
	public ArrayList<DBRecord> getModuleDetailsByteacher(int moduleId, String userid) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();
		System.out.println("$$$$$$$$$$$");
		// System.out.println(userid);
		try {
			String sql = "select * from exp_module where questionid in "
					+ "(select questionid from paper_exp where paperid in "
					+ "(select paperid from PAPER_INFO,DB_COURSE_TEACHER " + "where DB_COURSE_TEACHER.teacherid='" + userid +"'"
					+ " and paper_info.ano=db_course_teacher.ano and isactive=1 and pmoduleid=" + moduleId + ")) " + "order by expid";
			// String sql="select * from exp_module where moduleid = " +
			// moduleId + " order by expid";
			System.err.println(sql);
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			System.err.println(records.size());
			statement.close();
			connection.close();
		} catch (SQLException e) {
			System.err.println("exception:" + e.getMessage());
			// e.printStackTrace();
		}

		return records;
	}
	@Override
	public String getPaperId(int moduleId,String userId){
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();
		String paperId = "";
		try {
			String sql =  "select * from PAPER_INFO,DB_COURSE_USER " + "where userid=" + userId
							+ " and paper_info.ano=db_course_user.ano and isactive=1 and pmoduleid=" + moduleId;
					// String sql="select * from exp_module where moduleid = " +
					// moduleId + " order by expid";
			
			System.out.println(sql);
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			statement.close();
			connection.close();
			} catch (SQLException e) {
				System.out.println("exception:" + e.getMessage());
				// e.printStackTrace();
			}

		paperId = records.get(0).getString("PAPERID");
		System.out.println(paperId);
		
		//return paperId;
		return paperId;
	}

	@Override
	public ArrayList<DBRecord> getModuleDetailsByModuleIdAndExpId(int moduleId, int expId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();

		try {
			String sql = "select * from exp_module where moduleid = " + moduleId + " and expid = " + expId;
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			statement.close();
			connection.close();
		} catch (SQLException e) {
			System.out.println("exception:" + e.getMessage());
			// e.printStackTrace();
		}

		return records;
	}

	@Override
	public ArrayList<DBRecord> getSceneDetails(int moduleId, String userid) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		ArrayList<DBRecord> records = new ArrayList<DBRecord>();
		// System.out.println(userid);
		try {
			String sql = "select * from exp_scene where sceneid in "
					+ "(select psceneid from PAPER_INFO,DB_COURSE_USER " + "where userid=" + userid
					+ " and paper_info.ano=db_course_user.ano and pmoduleid=" + moduleId + ")";
			// String sql="select * from exp_module where moduleid = " +
			// moduleId + " order by expid";
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			records = ParseUtil.toDBRecord(resultSet);
			statement.close();
			connection.close();
		} catch (SQLException e) {
			System.out.println("exception:" + e.getMessage());
			// e.printStackTrace();
		}

		return records;
	}

}
